﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using Telerik.WinControls.UI;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class ViewDanhMucChiTiet : Form
    {
        int parentId;

        public ViewDanhMucChiTiet()
        {
            InitializeComponent();
        }

        public ViewDanhMucChiTiet(int parentId)
        {
            InitializeComponent();
            this.parentId = parentId;
        }

        private void SetPreferences()
        {
            radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            radGridView1.ShowGroupPanel = false;
            radGridView1.MasterTemplate.EnableGrouping = false;
            radGridView1.EnableHotTracking = true;
        }

        private void BuildData()
        {
            radGridView1.MasterTemplate.AllowAddNewRow = false;
            radGridView1.MasterTemplate.AutoGenerateColumns = false;

            GridViewSummaryItem summaryTrang = new GridViewSummaryItem("Trang", "Tổng: {0: #,#}", GridAggregateFunction.Sum);            
            GridViewSummaryItem summaryAnh = new GridViewSummaryItem("Anh", "Tổng: {0: #,#}", GridAggregateFunction.Sum);
            GridViewSummaryItem summaryHinh = new GridViewSummaryItem("Hinh", "Tổng: {0: #,#}", GridAggregateFunction.Sum);
            GridViewSummaryItem summaryManh = new GridViewSummaryItem("Manh", "Tổng: {0: #,#}", GridAggregateFunction.Sum);
            GridViewSummaryItem summaryBan = new GridViewSummaryItem("Ban", "Tổng: {0: #,#}", GridAggregateFunction.Sum);

            GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem(new GridViewSummaryItem[] { summaryTrang, summaryAnh, summaryHinh, summaryManh, summaryBan });
            this.radGridView1.SummaryRowsTop.Add(summaryRowItem);

            var biz = new DanhMucChiTietBiz();
            var items = biz.GetAllChiTiet(parentId);
            radGridView1.MasterTemplate.DataSource = items;

            if (items.Count == 0)
            {
                buttonEdit.Enabled = false;
                buttonRemove.Enabled = false;
            }
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                //Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.TaiLieuVanBanPhapLy);
                parent.Status.Text = Constants.FormStatus.DanhMucChiTiet;
            }

            SetPreferences();
            BuildData();

            CheckSecurity();
        }

        private void Edit(object sender, EventArgs e)
        {
            var dataRowInfo = radGridView1.CurrentRow as GridViewDataRowInfo;
            if (dataRowInfo != null)
            {
                var id = (int) dataRowInfo.Cells[12].Value;

                Utilities.CloseForm(this, new NewDanhMucChiTiet(parentId, id));
            }
        }

        private void Remove(object sender, EventArgs e)
        {
            if (MessageBox.Show(Constants.Messages.DeleteConfirm, Constants.Messages.DeleteCaption, MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                var items = new List<DanhMucChiTiet>();

                foreach (var row in radGridView1.Rows)
                {
                    if (row.Cells[0].Value != null && (bool)row.Cells[0].Value)
                    {
                        var temp = row.DataBoundItem as DanhMucChiTietInfo;
                        var item = new DanhMucChiTiet();
                        item.Id = temp.Id;
                        items.Add(item);
                    }
                }

                var biz = new DanhMucChiTietBiz();
                biz.RemoveItem(items);
            }

            Utilities.CloseForm(this, new ViewDanhMucChiTiet(parentId));
        }

        private void NewItem(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new NewDanhMucChiTiet(parentId));
        }

        private void CheckSecurity()
        {
            var parrent = MdiParent as MDI;

            if (parrent == null) return;

            if (parrent.CurrentGuest != null)
            {
                buttonEdit.Visible = false;
                buttonRemove.Visible = false;
                buttonNew.Visible = false;                
            }
        }

        private void BackToParentItem(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new NewDanhMuc(parentId));
        }      
        
    }
}
